package com.tianqu.tms.www.car.controller;

import java.util.ArrayList;
import java.util.List;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.www.car.action.CarGatesInfoIOResultAction;
import com.tianqu.tms.www.car.actionform.CarGatesInfoIOInputForm;

/**
 * 
 * 车辆进出库查询结果页面Controller
 * 
 * @author tianshifu
 *
 */
public class CarGatesInfoIOResultController extends TmsController {
	
	private Logger logger = Logger.getLogger(CarGatesInfoIOResultController.class);

	public static final String PAGING = "paging";
	
	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controllor = TxController.getTxController();

		//添加事务提交异常捕获
		try {
			controllor.begin();
			//从前台传过来一个form表单
			CarGatesInfoIOInputForm seriorForm = (CarGatesInfoIOInputForm) uc.getInputForm();
			
			Car car = Car.getInstance();
			
			 if(uc.getAttribute(CarGatesInfoIOResultAction.PAGENO) != null) {
				 car.setPageNo(Integer.parseInt((String)uc.getAttribute(CarGatesInfoIOResultAction.PAGENO)));
				}
				if(uc.getAttribute(CarGatesInfoIOResultAction.MAXINDEX) != null) {
					car.setMaxIndex(Integer.parseInt((String)uc.getAttribute(CarGatesInfoIOResultAction.MAXINDEX)));
				}
				if(uc.getAttribute(CarGatesInfoIOResultAction.SORTKEY) != null) {
					car.setSortKey(Integer.parseInt((String)uc.getAttribute(CarGatesInfoIOResultAction.SORTKEY)));
				}
				if(uc.getAttribute(CarGatesInfoIOResultAction.ASC) != null) {
					car.setAsc((new Boolean((String)uc.getAttribute(CarGatesInfoIOResultAction.ASC))).booleanValue());
				}
				
				
			List list = new ArrayList();
			if(uc.getAttribute("seniorFrom")==null)
			{
				//判定翻页
				list = car.findGatesInfoIOList(seriorForm.getCardNo(), seriorForm.getCarLicense(), seriorForm.getPeriod());
			}else{
				seriorForm = (CarGatesInfoIOInputForm)uc.getAttribute("seniorFrom");
				list = car.findGatesInfoIOList(seriorForm.getCardNo(), seriorForm.getCarLicense(), seriorForm.getPeriod());
			}
			
			uc.setAttribute(TmsConstants.VIEWHELP, list);
			uc.setAttribute(PAGING, car);
			//uc.removeAttribute("seniorFrom");
			uc.setAttribute("seniorFrom", seriorForm);
			logger.debug(this.getClass().getName() + "#execute was completed.");
			controllor.commit();
		} catch (Exception e) {
			try {
				controllor.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR,e1);
			}
			//系统异常
			if(e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR,e);
		}

	}
}
